package com.udaos.modules.field.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.udaos.models.field.Ud_field_photo;
import com.udaos.models.field.Ud_temp_field;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class UdFieldUtils {

	// 高德地图WEB_API 秘钥
	public static final String WEB_KEY = "3aa09806934c419299e8a6c93ad752d8";
	// 返回结果个数
	public static final String OFFSET = "50";

	/**
	 * 获取场地
	 * 
	 * @param keywords
	 *            关键字
	 * @param types
	 *            POI类型
	 * @param city
	 *            城市名称或编码
	 * @param page
	 *            当前页数
	 * @return
	 */
	public static JSONObject getTrainField(String keywords, String types, String city, String page) {
		String url = "http://restapi.amap.com/v3/place/text?key=KEY&keywords=WORDS&types=TYPES&city=CITY&children=1&offset=OFFSET&page=PAGE&extensions=all";
		url = url.replace("KEY", WEB_KEY);
		url = url.replace("WORDS", keywords);
		url = url.replace("TYPES", types);
		url = url.replace("CITY", city);
		url = url.replace("OFFSET", OFFSET);
		url = url.replace("PAGE", page);
		String s = HttpUtil.httpRequest(url);
		JSONObject j = JSONObject.fromObject(s);
		return j;
	}

	/**
	 * 数据组装
	 * 
	 * @param obj
	 * @param city
	 * @return
	 */
	public static Map<String, Object> getTrainFieldList(String city, String keywords, String types) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Ud_temp_field> list = new ArrayList<Ud_temp_field>();
		List<Ud_field_photo> photolist = new ArrayList<Ud_field_photo>();

		int readpage = 1;
		while (true) {
			// 取数
			String url = "http://restapi.amap.com/v3/place/text?key=KEY&keywords=WORDS&types=TYPES&city=CITY&children=1&offset=OFFSET&page=PAGE&extensions=all";
			url = url.replace("KEY", WEB_KEY);
			url = url.replace("WORDS", keywords);
			url = url.replace("TYPES", types);
			url = url.replace("CITY", city);
			url = url.replace("OFFSET", OFFSET);
			url = url.replace("PAGE", readpage + "");
			String s = HttpUtil.httpRequest(url);
			JSONObject obj = JSONObject.fromObject(s);
			// 获取相关数据
			int status = obj.getInt("status");
			if (status == 1) {
				// 获取查询结果数量
				int count = obj.getInt("count");
				if (count > 0) {
					// 计算页数
					int page = count / 50;
					int b = count % 50;
					if (b != 0) {
						page = page + 1;
					}
					// id去重
					List ids = new ArrayList();
					// 组装数据
					// 获取数据
					JSONArray pois = obj.getJSONArray("pois");
					if (readpage > page) {
						break;
					}
					readpage++;
					for (int j = 0; j < pois.size(); j++) {
						JSONObject data = pois.getJSONObject(j);
						String id = data.getString("id").trim();
						if (!ids.contains(id)) {
							ids.add(id);
							Ud_temp_field temp = new Ud_temp_field();
							temp.setId(data.getString("id"));
							temp.setState("0");
							temp.setName(data.getString("name"));
							temp.setType(data.getString("type"));
							temp.setTypecode(data.getString("typecode"));
							temp.setAddress(data.getString("address"));
							String location = data.getString("location");
							String[] locations = location.split(",");
							temp.setLng(Double.valueOf(locations[0]));
							temp.setLat(Double.valueOf(locations[1]));
							String telStr = data.getString("tel");
							if (telStr.length() < 3) {
								telStr = "";
							}
							temp.setTel(telStr);
							String postcodeStr = data.getString("postcode");
							if (postcodeStr.length() < 3) {
								postcodeStr = "";
							}
							temp.setPostcode(postcodeStr);
							temp.setPcode(data.getString("pcode"));
							temp.setPname(data.getString("pname"));
							temp.setCitycode(data.getString("citycode"));
							temp.setCityname(data.getString("cityname"));
							temp.setAdcode(data.getString("adcode"));
							temp.setAdname(data.getString("adname"));
							list.add(temp);
							// 組裝圖片数据
							String[] dd = JsonUtils.getStringArray4Json(data.getString("photos"));
							if (dd.length > 0) {
								for (int g = 0; g < dd.length; g++) {
									Ud_field_photo field_photo = new Ud_field_photo();
									field_photo.setPoi_id(data.getString("id"));
									JSONObject urlObj = JSONObject.fromObject(dd[g]);
									field_photo.setUrl(urlObj.getString("url"));
									photolist.add(field_photo);
								}
							} else {
								Ud_field_photo field_photo = new Ud_field_photo();
								field_photo.setPoi_id(data.getString("id"));
								photolist.add(field_photo);
							}
						}
					}
				}else{
					break;
				}
			}
		}
		map.put("list", list);
		map.put("photolist", photolist);
		return map;
	}

	public static void main(String[] args) {
		//JSONObject j = UdFieldUtils.getTrainField("驾校", "141500", "沙坪坝", "1");
		Map<String, Object> map = UdFieldUtils.getTrainFieldList("重庆","驾校","141500");
		System.out.println(map);
		List<Ud_temp_field> ll = (List<Ud_temp_field>) map.get("list");
		List<Ud_field_photo> ls = (List<Ud_field_photo>) map.get("photolist");
		System.out.println(ll.size());
		System.out.println(ls.size());
	}

}
